﻿#region License
// Copyright (c) 2009 Shengmin Zhang
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Web;
using System.IO;

using Apprender.Compression;
using Apprender.Web;

namespace Apprender.Extensions.Methods
{
    public static class HttpExtension
    {
        public static CompressionEncoding GetAcceptEncoding(this HttpRequest request)
        {
            var value = request.Headers.GetValue("Accept-Encoding");

            CompressionEncoding encoding = CompressionEncoding.None;
            if (value.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                encoding = CompressionEncoding.GZip;
            }
            /*
            if (value.IndexOf("deflate", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                encoding = encoding | CompressionEncoding.Deflate;
            }
             * */
            return encoding;            
        }

        /// <summary>
        /// Get the input content type
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static HttpRequestContentType GetContentType(this HttpRequest request)
        {
            // If it's "GET" method, then the input is part of the query string.
            if (request.HttpMethod[0] == 'G' || request.HttpMethod[0] == 'g')
            {
                if (request.QueryString.Count > 0)
                    return HttpRequestContentType.QueryString;
                else
                    return HttpRequestContentType.None;
            }
            // If it's "POST" method, then the input is part of the body
            else if (request.HttpMethod[0] == 'P' || request.HttpMethod[0] == 'p')
            {
                if (request.InputStream.Length > 0)
                {
                    if (request.Form.Count > 0)
                        return HttpRequestContentType.Form;
                    else
                        return HttpRequestContentType.Json;
                }
                else
                {
                    return HttpRequestContentType.None;
                }
            }

            return HttpRequestContentType.Unknown;
        }

        public static string GetInput(this HttpRequest request)
        {
            string input = string.Empty;            
            TextReader rd = new StreamReader(request.InputStream);
            try
            {
                input = rd.ReadToEnd();
            }
            finally
            {
                rd.Close();
            }
            return input;
        }

    }
}
